#$HeadURL: http://sscreen.googlecode.com/svn/trunk/menu.py $
#$Id: menu.py 18 2008-09-03 23:27:42Z mateusz.kijowski $

# Copyright (C) 2007,2008  Mateusz Kijowski <mateusz.kijowski@gmail.com>

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import curses
import curses.wrapper

def __init__():
    pass

class menu:
    HOTKEYS=[
    '1','2','3','4','5','6','7','8','9','0','-','=',
    '!','@','#','$','%','^','&','*','(',')','_','+',
    ]
    def __init__(self,menu_dict_list):
        self.restart=0
        self.items= [
                     menuItem(m['txt'], menu_dict_list.index(m), m['fun'], m['args']) 
                     for m in menu_dict_list 
                    ]
	self.hotkeylist = {}
    def dostart(self):
        curses.wrapper(self._start)
    def dorestart(self):
        self.restart=1
    def _start(self,stdscr):
        curses.use_default_colors()
        vtab=10
        htab=20
        maxlen=65
        
        #set title
        stdscr.addstr(0,1,sys.argv[0],curses.A_NORMAL)
        stdscr.refresh()
        
        self.win=curses.newwin(len(self.items)+1,maxlen,vtab,htab)
        self.win.keypad(1)

        self.previous_item = self.items[0]
        self.current_item = self.items[0]
        for item in self.items:
            item.set_win(self.win)
            item.display()
	    self.hotkeylist[ord(menu.HOTKEYS[item.number])]=item
        self.current_item.activate()
        while not self.restart:
            c = self.win.getch()
            if c == ord('q'):
                break
            elif c == curses.KEY_UP:
                self.previous_item = self.current_item
                if self.current_item.number == 0:
                    self.current_item = self.items[len(self.items) -1 ]
                else:
                    self.current_item = self.items[self.current_item.number - 1]
                self.update()                    
            elif c == curses.KEY_DOWN:
                self.previous_item = self.current_item
                if self.current_item.number == len(self.items) -1:
                    self.current_item = self.items[0]
                else:
                    self.current_item = self.items[self.current_item.number + 1]
                self.update()              
# Why doesn't it work with curses.KEY_ENTER ?
            elif c in (curses.KEY_ENTER,10):
                self.current_item.run()
                self.win.refresh()
            elif self.hotkeylist.has_key(c):
                self.hotkeylist[c].run()
                self.win.refresh()
        curses.beep()
    def update(self):
        self.previous_item.deactivate()
        self.current_item.activate()

class menuItem():
    def __init__(self,txt,number,function,args):
        self.activated=0
        self.txt=txt
        self.number=number
        self.function=function
        self.args=args
        self.win=0
    def run(self):
        apply(self.function,self.args)
    def set_win(self,win):
        self.win=win
    def activate(self):
        self.activated=1
        self.display()
    def deactivate(self):
        self.activated=0
        self.display()
    def display(self):
        self.win.addstr(self.number,0,"%s: %s" % (menu.HOTKEYS[self.number],self.txt),
                        self.activated == 1 and curses.A_REVERSE or curses.A_NORMAL)
if __name__ == "__main__":
    menu_dict_list = [
              dict(txt="pierwsze", fun=getattr(os,"system"), args=("screen -t nowy")),
              dict(txt="drugie", fun=getattr(os,"system"), args=("screen -t nowy")),
              dict(txt="trzecie", fun=getattr(os,"system"), args=("screen -t nowy")),
              dict(txt="czwarte", fun=getattr(os,"system"), args=("screen -t nowy"))
              ]
    print menu_dict_list
    m = menu(menu_dict_list)
